#include <iostream>
#include "Comm.hpp"



int main()
{
    //1.创建key
    key_t k = ::ftok(gpath.c_str(),gprojId);
    if(k < 0)
    {
        std::cerr << "ftok error" << std::endl;
        return 1;
    }
    std::cout << "k : " << k << std::endl;
    //2.创建共享内存 && 获取
    // 注意: 共享内存也有权限!
    int shmid = ::shmget(k,gshmsize,IPC_CREAT | IPC_EXCL | gmode);
    if(shmid < 0)
    {
        std::cerr << "shmget error" << std::endl;
        return 2;
    }
    std::cout << "shmid : " << shmid << std::endl;
    sleep(5);

    //3.共享内存挂接到自己的地址空间中
    void* ret = shmat(shmid,nullptr,0);
    std::cout << "attach done: " << (long long)ret << std::endl;
    sleep(5);


    ::shmdt(ret);
    std::cout << "detach done..." << std::endl;

    //n.删除共享内存
    shmctl(shmid,IPC_RMID,nullptr);
    std::cout << "deete shm done" << std::endl;
    sleep(5);
    return 0;
}
